package com.polaris.live.viewmodel.me

import androidx.lifecycle.MutableLiveData
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.network.state.ResultState
import com.polaris.live.common.resp.UserResp
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.net.gameService
import com.polaris.live.net.userService
import com.polaris.live.resp.back_resp.CallPriceResp
import com.polaris.live.resp.back_resp.CustomerMessageUnreadResp
import com.polaris.live.resp.back_resp.GameGroupVoResp
import com.polaris.live.resp.go_resp.ChatPriceBean
import com.polaris.live.ui.game.GameFromType
import com.polaris.live.ui.game.GameUtils

/**
 * MeViewModel
 *
 * @author Created by 半仙 on 2023/08/29 07:00
 * @since 1.0.0
 */
class MeViewModel : BaseViewModel() {

    val mUserLiveData = MutableLiveData<UserResp>()
    fun getUser() {
        request({ userService.getUserData() }, {
            UserManager.setUser(it)
            mUserLiveData.value = it
        })
    }

    val mCallPriceLiveData = MutableLiveData<ResultState<CallPriceResp>>()

    /**
     * 获取通话价格
     */
    fun getCallPrice() {
        request({ userService.getCallPrice() }, mCallPriceLiveData)
    }

    val mModifyCallPriceLiveData = MutableLiveData<Any>()

    /**
     * 修改通话价格
     */
    fun modifyCallPrice(callPrice: Long) {
        requestLoading({ userService.modifyCallPrice(ChatPriceBean(callPrice)) }, {
            mModifyCallPriceLiveData.value = it
        }, {
            ToastGravity.showGravity(it.errorMsg)
        })
    }

    //获取游戏列表
    val gameLiveData = MutableLiveData<ResultState<List<GameGroupVoResp>>>()
    fun getGameList() {
        //已经重试完成，每次打开界面在没有数据时再刷新
        if (GameUtils.getMeCacheGameList().isNullOrEmpty() && GameUtils.retryCount < GameUtils.MAX_RETRY_COUNT) {
            request({ gameService.getGameList(GameFromType.ME) }, gameLiveData)
        }
    }

    val mBackpackNewLiveData = MutableLiveData<ResultState<Boolean>>()

    /**
     * getBackpackNew
     */
    fun getBackpackNew() {
        request({ userService.getBackpackNew(null, null) }, mBackpackNewLiveData)
    }

    /**
     * 客服红点消息
     */
    val mCustomerReadLiveData = MutableLiveData<ResultState<CustomerMessageUnreadResp>>()
    fun getCustomerRead() {
        request({ userService.customerServiceUnread() }, mCustomerReadLiveData)
    }
}